#!/bin/bash
#
# Install and start memcached server
#
# 07/29/2012 - created

TMP_DIR=/tmp
MCDB_HOSTS="$@"


function showBar {
 percDone=$(echo 'scale=2;'$1/$2*100 | bc)
 barLen=$(echo ${percDone%'.00'})
 bar=''
 fills=''
 for (( b=0; b<$barLen; b++ ))
 do
  bar=$bar"="
 done
 blankSpaces=$(echo $((100-$barLen)))
 for (( f=0; f<$blankSpaces; f++ ))
 do
  fills=$fills"."
 done
 echo -ne '['$bar'>'$fills']: '$barLen'%\r'
}



echo
gcutil addinstance --image=projects/google/images/centos-6-2-v20120621 --machine_type=n1-standard-1 --zone=us-central1-a ${MCDB_HOSTS}


echo
echo "Waiting for VMs creation to complete...."
for (( i=0; i<=9600; i++ ))
do
 showBar $i 9600
done
echo


# setup memcached instances
for mc in ${MCDB_HOSTS}
do
     gcutil push ${mc} getmcdb.sh ${TMP_DIR}
     gcutil ssh ${mc} ${TMP_DIR}/getmcdb.sh
done


# report memcached stats for each instance
for mc in ${MCDB_HOSTS}
do
    echo
    echo "Memcached stats...."
    echo
    gcutil ssh ${mc} /usr/bin/memcached-tool 127.0.0.1:11211 stats
    echo
done

exit
